package org.lq.ssm.service.impl;

import org.lq.ssm.dto.RoleDTO;
import org.lq.ssm.dto.UserDTO;
import org.lq.ssm.dto.mapper.RoleDTOMapper;
import org.lq.ssm.mapper.*;
import org.lq.ssm.po.*;
import org.lq.ssm.service.RoleService;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RolePOMapper rolePOMapper;
    @Autowired
    PermissionPOMapper permissionPOMapper;
    @Autowired
    DeptPOMapper deptPOMapper;
    @Autowired
    MenuPOMapper menuPOMapper;

    @Autowired
    RolesPermissionsMapper rolesPermissionsMapper;
    @Autowired
    RolesDeptsMapper rolesDeptsMapper;
    @Autowired
    RolesMenusMapper rolesMenusMapper;



    @Override
    public List<RoleDTO> getAllRole() {
        RolePOExample rolePOExample = new RolePOExample();
        RolePOExample.Criteria criteria = rolePOExample.createCriteria();
        criteria.getAllCriteria();
        List<RolePO> rolePOS = rolePOMapper.selectByExample(rolePOExample);
        List<RoleDTO> roleDTOS = new LinkedList<>();
        for (RolePO rolePO : rolePOS){
            List<PermissionPO> permissions = getPermissions(rolePO);
            List<DeptPO> depts = getDepts(rolePO);
            List<MenuPO> menus = getMenus(rolePO);
            RoleDTO roleDTO = RoleDTOMapper.INSTANCE.RolePOToRoleDTO(rolePO, depts, permissions, menus);
            roleDTOS.add(roleDTO);
        }

        return roleDTOS;
    }
    @Override
    public List<PermissionPO> getPermissions(RolePO rolePO){
        RolesPermissionsExample rpExample = new RolesPermissionsExample();
        RolesPermissionsExample.Criteria criteria = rpExample.createCriteria();
        criteria.andRoleIdEqualTo(rolePO.getId());
        List<RolesPermissionsKey> rpKeys = rolesPermissionsMapper.selectByExample(rpExample);
        List<PermissionPO> permissionPOS = new LinkedList<>();
        for (RolesPermissionsKey rpKey : rpKeys){
            Long permissionId = rpKey.getPermissionId();
            PermissionPO permissionPO = permissionPOMapper.selectByPrimaryKey(permissionId);
            permissionPOS.add(permissionPO);
        }
        return permissionPOS;
    }

    List<DeptPO> getDepts(RolePO rolePO){
        RolesDeptsExample rolesDeptsExample = new RolesDeptsExample();
        RolesDeptsExample.Criteria criteria = rolesDeptsExample.createCriteria();
        criteria.andRoleIdEqualTo(rolePO.getId());
        List<RolesDeptsKey> rolesDeptsKeys = rolesDeptsMapper.selectByExample(rolesDeptsExample);
        List<DeptPO> deptPOS = new LinkedList<>();
        for (RolesDeptsKey rdKey : rolesDeptsKeys){
            DeptPO deptPO = deptPOMapper.selectByPrimaryKey(rdKey.getDeptId());
            deptPOS.add(deptPO);
        }
        return deptPOS;
    }
    private List<MenuPO> getMenus(RolePO rolePO){
        RolesMenusExample rolesMenusExample = new RolesMenusExample();
        RolesMenusExample.Criteria criteria = rolesMenusExample.createCriteria();
        criteria.andRoleIdEqualTo(rolePO.getId());
        List<RolesMenusKey> rolesMenusKeys = rolesMenusMapper.selectByExample(rolesMenusExample);
        List<MenuPO> menuPOS = new LinkedList<>();
        for (RolesMenusKey rolesMenusKey : rolesMenusKeys){
            MenuPO menuPO = menuPOMapper.selectByPrimaryKey(rolesMenusKey.getMenuId());
            menuPOS.add(menuPO);
        }
        return menuPOS;
    }
}
